package com.po;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * BaseCode entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.po.BaseCode
 * @author MyEclipse Persistence Tools
 */

public class BaseCodeDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(BaseCodeDAO.class);
	// property constants
	public static final String MAIN_AREA = "mainArea";
	public static final String MINOR_AREA = "minorArea";
	public static final String CHINESE_NAME = "chineseName";
	public static final String ENGLISH_NAME = "englishName";
	public static final String DEFINE = "define";
	public static final String REMARK = "remark";

	public void save(BaseCode transientInstance) {
		log.debug("saving BaseCode instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(BaseCode persistentInstance) {
		log.debug("deleting BaseCode instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public BaseCode findById(java.lang.String id) {
		log.debug("getting BaseCode instance with id: " + id);
		try {
			BaseCode instance = (BaseCode) getSession().get("com.po.BaseCode",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(BaseCode instance) {
		log.debug("finding BaseCode instance by example");
		try {
			List results = getSession().createCriteria("com.po.BaseCode").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding BaseCode instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from BaseCode as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByMainArea(Object mainArea) {
		return findByProperty(MAIN_AREA, mainArea);
	}

	public List findByMinorArea(Object minorArea) {
		return findByProperty(MINOR_AREA, minorArea);
	}

	public List findByChineseName(Object chineseName) {
		return findByProperty(CHINESE_NAME, chineseName);
	}

	public List findByEnglishName(Object englishName) {
		return findByProperty(ENGLISH_NAME, englishName);
	}

	public List findByDefine(Object define) {
		return findByProperty(DEFINE, define);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findAll() {
		log.debug("finding all BaseCode instances");
		try {
			String queryString = "from BaseCode";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public BaseCode merge(BaseCode detachedInstance) {
		log.debug("merging BaseCode instance");
		try {
			BaseCode result = (BaseCode) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(BaseCode instance) {
		log.debug("attaching dirty BaseCode instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(BaseCode instance) {
		log.debug("attaching clean BaseCode instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}